/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.projects.toolbox_basics_project.dependency;

import com.genia.toolbox.persistence.exception.PersistenceException;
import com.genia.toolbox.persistence.spring.dependency.impl.AbstractPersistableDependency;
import com.genia.toolbox.projects.toolbox_basics_project.bean.model.BasicOrdered;
import com.genia.toolbox.projects.toolbox_basics_project.spring.manager.OrderedManager;

/**
 * dependency class that handle the instanciation of the order property of the
 * orderable object.
 */
public class OrderedModificationDependency
    extends AbstractPersistableDependency<BasicOrdered>
{

  /**
   * the {@link OrderedManager} to use.
   */
  private OrderedManager orderedManager;



  /**
   * constructor.
   */
  public OrderedModificationDependency()
  {
    super(BasicOrdered.class);
  }



  /**
   * handle the dependencies before creating an object.
   * 
   * @param object
   *          the object that will be created
   * @throws PersistenceException
   *           when an error occured
   * @see com.genia.toolbox.persistence.spring.dependency.impl.AbstractPersistableDependency#onCreate(java.lang.Object)
   */
  @Override
  public void onCreate(BasicOrdered object)
      throws PersistenceException
  {
    onModify(object);
  }



  /**
   * handle the dependencies before updating an object.
   * 
   * @param object
   *          the object that will be updated
   * @throws PersistenceException
   *           when an error occured
   * @see com.genia.toolbox.persistence.spring.dependency.impl.AbstractPersistableDependency#onUpdate(java.lang.Object)
   */
  @Override
  public void onUpdate(BasicOrdered object)
      throws PersistenceException
  {
    onModify(object);
  }



  /**
   * method called when an object is saved or update.
   * 
   * @param object
   *          the object being saved or update
   * @throws PersistenceException
   *           when an error occured
   */
  private void onModify(BasicOrdered object)
      throws PersistenceException
  {
    if (object.getOrder() == 0) {
      object.setOrder(getOrderedManager().getNewMaxOrder(object.getClass()));
    }
  }



  /**
   * getter for the orderedManager property.
   * 
   * @return the orderedManager
   */
  public OrderedManager getOrderedManager()
  {
    return orderedManager;
  }



  /**
   * setter for the orderedManager property.
   * 
   * @param orderedManager
   *          the orderedManager to set
   */
  public void setOrderedManager(OrderedManager orderedManager)
  {
    this.orderedManager = orderedManager;
  }
}
